<template>
  <div class="page-modal">
      <el-dialog v-model="dialogVisible" title="Warning" width="30%" center  destroy-on-close>
        <hy-form v-bind="modalConfig" v-model="formData"></hy-form>
        <slot></slot>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleConfirmClick"
              >确定</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
</template>

<script lang="ts">
import { defineComponent , ref, watch} from "vue";
import HyForm from '@/base-ui/form'
import { useStore } from "@/store"
import { useRoute } from "vue-router";

export default defineComponent({
  components:{
    HyForm
  },
  props:{
    modalConfig:{
      type:Object,
      defailt:()=>({}),
      require:true
    },
    defaultInfo:{
      type:Object,
      default:()=>({})
    },
    pageName:{
      type:String,
      default:''
    },
    //其他参数
    otherInfo:{
      type:Object,
      default:() => ({})
    }
  },
  setup(props) {
    const dialogVisible = ref(false)
    const formData = ref<any>({})

    watch(
      () => props.defaultInfo,
      (newVal) => {
        // console.log(newVal)

        for(const item of props.modalConfig?.formItems){
          formData.value[`${item.field}`] = newVal[`${item.field}`]
        }


      }
    )
    const store = useStore();
    //点击确定按钮的逻辑
    const handleConfirmClick = () => {
      dialogVisible.value = false
      //判断是新建还是编辑
      if(Object.keys(props.defaultInfo).length){
        //编辑
        store.dispatch('system/editPageDataAction',{
          pageName:props.pageName,
          editData:{...formData.value,...props.otherInfo},
          id:props.defaultInfo.id
        })
      }else{
        //新建
        store.dispatch('system/createPageDataAction',{
          pageName:props.pageName,
          newData:{...formData.value,...props.otherInfo}
        })
      }
    }

    return {
       dialogVisible,
       formData,
       handleConfirmClick,
    }
  }
})
</script>

<style lang="less" scoped>

</style>
